local skels = {}

local xuanhuo = fk.CreateSkill{
    name = "XD__xuanhuo",
}
Fk:loadTranslationTable{
    ["XD__xuanhuo"] = "眩惑",
    [":XD__xuanhuo"] = "出牌阶段开始时，你可以选择一名角色，视为其使用一张【声东击西】，且目标由你选择。",
    ["#XD__xuanhuo"] = "是否发动 眩惑，令一名角色视为对你指定的角色使用一张【声东击西】",
    ["#XD__xuanhuo-choose"] = "眩惑：请选择 %dest 使用【声东击西】的目标",
    ["XD__xuanhuo_active"] = "眩惑",
    ["XD_xuanhuo__diversion_skill"] = "声东击西",
    ["XD__xuanhuo_tip_1"] = "获得一张牌",
    ["XD__xuanhuo_tip_2"] = "获得两张牌",
}
xuanhuo:addEffect(fk.EventPhaseStart, {
    anim_type = "control",
    can_trigger = function (self, event, target, player, data)
        return target == player and player:hasSkill(xuanhuo.name) and player.phase == Player.Play
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local targets = table.filter(room.alive_players, function (cp)
            local card = Fk:cloneCard("diversion")
            card.skillName = xuanhuo.name
            return table.find(room.alive_players, function (tp)
                return cp:canUseTo(card, tp)
            end) ~= nil
        end)
        if #targets > 0 then
            local tos = room:askToChoosePlayers(player, {
                targets = targets,
                skill_name = xuanhuo.name,
                prompt = "#XD__xuanhuo",
                max_num = 1,
                min_num = 1,
            })
            if #tos > 0 then
                event:setCostData(self,{tos = tos})
                return true
            end
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local to = event:getCostData(self).tos[1]
        local card = Fk:cloneCard("diversion")
        card.skillName = xuanhuo.name
        room:setPlayerMark(player, xuanhuo.name, to.id)
        local bool, dat = room:askToUseActiveSkill(player, {
            skill_name = "XD__xuanhuo_active",
            prompt = "#XD__xuanhuo-choose::"..to.id
        })
        room:setPlayerMark(player, xuanhuo.name, 0)
        if bool and dat then
            room:useCard({
                card = card,
                from = to,
                tos = dat.targets,
                --subTos = {{dat.targets[2]}}
            })
        end
    end,
})
--[[xuanhuo:addEffect(fk.PreCardEffect, {
    can_refresh = function (self, event, target, player, data)
        return data.card.skillName == xuanhuo.name
    end,
    on_refresh = function (self, event, target, player, data)
        data:changeCardSkill("XD_xuanhuo__diversion_skill")
    end,
})]]--
table.insert(skels, xuanhuo)

local xuanhuo_active = fk.CreateSkill{
    name = "XD__xuanhuo_active"
}
xuanhuo_active:addEffect("active", {
    card_num = 0,
    card_filter = Util.FalseFunc,
    target_num = 1,
    target_filter = function (self, player, to_select, selected, selected_cards, card, extra_data)
        local from = player:getMark(xuanhuo.name)
        if #selected == 0 and from ~= 0 then
            local c = Fk:cloneCard("diversion")
            c.skillName = xuanhuo.name
            return Fk:currentRoom():getPlayerById(from):canUseTo(c, to_select)
        end
    end,
    --[[target_tip = function(self, _, to_select, selected, _, _, selectable, _)
        if not selectable then return end
        if #selected == 0 or (#selected > 0 and selected[1] == to_select) then
            return "XD__xuanhuo_tip_1"
        else
            return "XD__xuanhuo_tip_2"
        end
    end,]]--
})
table.insert(skels, xuanhuo_active)

--[[local diversion_skill = fk.CreateSkill{
    name = "XD_xuanhuo__diversion_skill",
}
diversion_skill:addEffect("cardskill", {
    mute = true,
    prompt = "#diversion_skill",
    distance_limit = 1,
    mod_target_filter = function(self, player, to_select, selected, card, extra_data)
        return to_select ~= player and
        ((extra_data and extra_data.bypass_distances) or self:withinDistanceLimit(player, false, card, to_select))
    end,
    target_filter = Util.CardTargetFilter,
    target_num = 1,
    on_effect = function(self, room, effect)
        local from = effect.from
        local to = effect.to
        local others = effect.subTargets
        if to.dead or from:isKongcheng() or #others == 0 then return end
        --table.removeOne(others, from)
        local plist, cid = room:askToChooseCardsAndPlayers(from, {
            min_card_num = 1,
            max_card_num = 1,
            min_num = 1,
            max_num = 1,
            targets = others,
            pattern = ".|.|.|hand",
            skill_name = "diversion_skill",
            prompt = "#diversion-give::" .. to.id,
            cancelable = false,
            no_indicate = true,
        })
        local target = plist[1]
        room:doIndicate(to, {target})
        room:moveCardTo(cid, Player.Hand, to, fk.ReasonGive, "diversion_skill", nil, false, from)
        if to.dead or target.dead then return end
        local card = to:getCardIds("he")
        if #card > 2 then
            card = room:askToCards(to, {
                min_num = 2,
                max_num = 2,
                include_equip = true,
                skill_name = "diversion_skill",
                prompt = "#diversion-give2::" .. target.id,
                cancelable = false,
            })
        end
        room:moveCardTo(card, Player.Hand, target, fk.ReasonGive, "diversion_skill", nil, false, from)
    end,
})
table.insert(skels, diversion_skill)]]--

return skels